

'use strict';

const express=require('express');
const path=require('path');
const xtpl=require('xtpl');
const moment=require('moment');

const db=require(path.join(__dirname,"../configs/database.js"));
const base=require(path.join(__dirname,"/baseController.js"));


module.exports.teaClassList=(req,res)=>{


 const page=parseInt(req.query.page||'1');
 const limit=parseInt(req.query.limit||'12');
 const start=(page-1)*limit;
 db.table('class').count("*").then((count)=>{
   const totalPage=count%limit==0?count/limit:parseInt(count/limit)+1;
   const totalPageArray=[];
   for(var i=1;i<=totalPage;i++){
      totalPageArray.push(i);
   }

   db.table('class').order('class_id DESC').limit(start,limit).select().then((data)=>{

    xtpl.renderFile(
      path.join(__dirname,"../views/admin/teaClass_list.html"),
      {
       array:data,
       totalPageArray:totalPageArray,
       page:page,
       totalPage:totalPage
      },
      (err,content)=>{
          if(err){
          	return console.log(err);
          }
          res.setHeader("Content-Type","text/html;charset=utf-8");
          res.end(content);
      }
  	);
      
   });
 });



};

module.exports.teaClassEdite=(req,res)=>{
  let class_id=req.params.class_id||0;
  let where=`class_id=${class_id}`;
  db.table('class').where(where).find().then((data)=>{
      db.table('teach').select().then((datas)=>{
      	if(JSON.stringify(data)=="{}"){

            base.showMsg(res,"班级不存在","/teaclass/teaClass_list");
            return false;

      	}else{
      
   data.class_time=moment(data.class_time*1000).format("YYYY-MM-DD");
   xtpl.renderFile(
    path.join(__dirname,"../views/admin/teaClass_edite.html"),
    {

      array:datas,
      classInfo:data,
    },
    (err,content)=>{
    	if(err){

    		return console.log(err);
    	}
      res.setHeader("Context-Type","text/html;charset=utf-8");
      res.end(content);

    }

 	);
      	}
      });
  });
};

module.exports.teaClassEditeData=(req,res)=>{
  let class_id=req.params.class_id||0;
  let where=`class_id=${class_id}`;
  db.table('class').where(where).find().then((data)=>{
      if(JSON.stringify(data)=="{}"){
           base.showMsg(res,'班级不存在','/teaClass/teaClass_list');
           return false;
      }else{

         let check=`class_name='${req.body.class_name}' AND class_id!=${class_id}`;
         db.table('class').where(check).find().then((data)=>{
               
               if(JSON.stringify(data)!="{}"){
               	base.showMsg(res,'班级已经存在');
               	return false;
               }else{

                let data={
                    
                    "class_name":req.body.class_name,
                    "teach_id":req.body.teach_id,
                    "class_time":(Date.parse( moment(req.body.class_time) )/1000)

                };

                db.table('class').where(`class_id=${class_id}`).update(data).then(
                     
                     (affectRows)=>{

                     res.redirect('/teaclass/teaClass_list');
                      return false;

                     }

                	).catch((err)=>{

                		base.showMsg(res,'更新失败');
                		return false;
                	});

               }
         });

      }

  });
};

